package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Autowired
	private BaseAttributeValueMapper baseAttributeValueMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		// 查所有并且分组
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//2、将上面查询到的数据组装 为页面要的数据
		//2.1 一级分类集合
		List<JSONObject> list1 = new ArrayList<>();
		//stream流groupby
		Map<Long, List<BaseCategoryView>> c1d = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		c1d.forEach((key, value) -> {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("categoryId", key);
			jsonObject.put("categoryName", value.get(0).getCategory1Name());

			List<JSONObject> list2 = new ArrayList<>();
			value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id)).forEach((key2, value2) -> {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", key2);
				jsonObject2.put("categoryName", value2.get(0).getCategory2Name());

				List<JSONObject> list3 = new ArrayList<>();

				value2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id)).forEach((key3, value3) -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", key3);
					jsonObject3.put("categoryName", value3.get(0).getCategory3Name());
					list3.add(jsonObject3);
				});
				jsonObject2.put("categoryChild", list3);
				list2.add(jsonObject2);
			});
			jsonObject.put("categoryChild", list2);
			list1.add(jsonObject);
		});
		// 每个一级分类的child接收自己的二级分类
		// 每个二级分类的child接收自己的三级分类集合
		return list1;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		List<BaseAttribute> baseAttributeList = new ArrayList<>();

		List<BaseAttribute> baseAttributeList1 = baseAttributeMapper.selectList(Wrappers.<BaseAttribute>lambdaQuery().eq(BaseAttribute::getCategory1Id, category1Id));

		if (baseAttributeList1 != null) {
			baseAttributeList1.forEach(baseAttribute -> {
				BaseAttribute baseAttribute1 = new BaseAttribute();
				baseAttribute1.setId(baseAttribute.getId());
				baseAttribute1.setAttributeName(baseAttribute.getAttributeName());

				List<BaseAttributeValue> baseAttributeValueList = new ArrayList<>();
				List<BaseAttributeValue> baseAttributeValues = baseAttributeValueMapper.selectList(Wrappers.<BaseAttributeValue>lambdaQuery().eq(BaseAttributeValue::getAttributeId, baseAttribute.getId()));

				if (baseAttributeValues != null) {
					baseAttributeValues.forEach(baseAttributeValue -> {
						BaseAttributeValue baseAttributeValue1 = new BaseAttributeValue();
						baseAttributeValue1.setId(baseAttributeValue.getId());
						baseAttributeValue1.setValueName(baseAttributeValue.getValueName());
						baseAttributeValue1.setAttributeId(baseAttributeValue.getAttributeId());
						baseAttributeValueList.add(baseAttributeValue1);
					});
				}

				baseAttribute1.setAttributeValueList(baseAttributeValueList);
				baseAttributeList.add(baseAttribute1);
			});
		}
		return baseAttributeList;
	}
}
